package animation3.view.com.zhaolixing.viewanimation3

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.animation.*
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bt_scale_animation.setOnClickListener{
            /**
             * 下面等价于
             * <?xml version="1.0" encoding="utf-8"?>
            <scale xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromXScale="0.0"
            android:toXScale="1.5"
            android:fromYScale="0.0"
            android:toYScale="1.5"
            android:pivotX="50"
            android:pivotY="50"
            android:fillAfter="true"
            android:duration="7000" />
             */
            val scaleAnimation= ScaleAnimation(1f,1.5f,1f,1.5f
            , Animation.ABSOLUTE,50f,Animation.ABSOLUTE,50f)
            scaleAnimation.duration=5000
            scaleAnimation.fillAfter=true
            view.startAnimation(scaleAnimation)
        }

        bt_alpha_animation.setOnClickListener{
            /**
             * 下面对面等效于
             * <?xml version="1.0" encoding="utf-8"?>
            <alpha xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromAlpha=0.5"
            android:toAlpha="1.0"
            android:duration="5000"
            android:fillAfter"true">
            </alpha>
             */
            val alphaAnimation= AlphaAnimation(0.5f,1f)
            alphaAnimation.duration=5000
            alphaAnimation.fillAfter=true
            view.startAnimation(alphaAnimation)
        }

        bt_rotate_animation.setOnClickListener{
            /**
             * 下面代码等效于：
             * <?xml version="1.0" encoding="utf-8"?>
            <rotate xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromDegrees="0"
            android:toDegrees="360
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="7000"
            android:fillAfter="true">
            </rotate>
             */
            val rotateAnimation=RotateAnimation(0f,360f,
                    Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f)
            rotateAnimation.duration=5000
            rotateAnimation.fillAfter=true
            view.startAnimation(rotateAnimation)
        }

        bt_translate_animation.setOnClickListener{
            /**
             * 下面代码等效于：
             * <?xml version="1.0" encoding="utf-8"?>
            <translate xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromXDelta="0"
            android:toXDelta="200"
            android:fromYDelta="0"
            android:toYDelta="200"
            android:duration="5000"
            android:fillAfter="true">
            </translate>
             */
            val translateAnimation=TranslateAnimation(Animation.ABSOLUTE,0f,
                    Animation.ABSOLUTE,200f,
                    Animation.ABSOLUTE,0f,
                    Animation.ABSOLUTE,200f)
            translateAnimation.duration=5000
            translateAnimation.fillAfter=true
            view.startAnimation(translateAnimation)
        }

        bt_set_animation.setOnClickListener{
            /**
             * 下面代码等效于：
             * <?xml version="1.0" encoding="utf-8"?>
            <set xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="7000"
            android:fillAfter="true">

            <alpha
            android:fromAlpha="0.5"
            android:toAlpha="1.0"/>

            <scale
            android:fromXScale="0.0"
            android:toXScale="1.5"
            android:fromYScale="0.0"
            android:toYScale="1.5"
            android:pivotX="50"
            android:pivotY="50"/>

            <rotate
            android:fromDegrees="0"
            android:toDegrees="360"
            android:pivotX="50%"
            android:pivotY="50%"/>

            </set>
             */
            val alphaAnimation=AlphaAnimation(0.5f,1f)
            val scaleAnimation=ScaleAnimation(1f,1.5f,1f,1.5f,
                    Animation.ABSOLUTE,50f,Animation.ABSOLUTE,50f)
            val rotateAnimation=RotateAnimation(0f,360f,
                    Animation.RELATIVE_TO_SELF,.5f,
                    Animation.RELATIVE_TO_SELF,0.5f)
            //取true时，指在AnimationSet中定义一个插值器（interpolater），
            // 它下面的所有动画共同。如果设为false，则表示它下面的动画自己定义各自的插值器。
            val setAnimation=AnimationSet(true)
            setAnimation.addAnimation(alphaAnimation)
            setAnimation.addAnimation(scaleAnimation)
            setAnimation.addAnimation(rotateAnimation)
            setAnimation.duration=5000
            setAnimation.fillAfter=true
            view.startAnimation(setAnimation)
        }

        bt_set_interpolator_animation.setOnClickListener{
            /**
             * 下面代码等效于：
             * <?xml version="1.0" encoding="utf-8"?>
            <set xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="7000"
            android:fillAfter="true">

            <alpha
            android:fromAlpha="0.5"
            android:toAlpha="1.0"/>

            <scale
            android:fromXScale="0.0"
            android:toXScale="1.5"
            android:fromYScale="0.0"
            android:toYScale="1.5"
            android:pivotX="50"
            android:pivotY="50"/>

            <rotate
            android:fromDegrees="0"
            android:toDegrees="360"
            android:pivotX="50%"
            android:pivotY="50%"/>

            </set>
             */
            val alphaAnimation=AlphaAnimation(0.5f,1f)
            val scaleAnimation=ScaleAnimation(1f,1.5f,1f,1.5f,
                    Animation.ABSOLUTE,50f,Animation.ABSOLUTE,50f)
            val rotateAnimation=RotateAnimation(0f,360f,
                    Animation.RELATIVE_TO_SELF,.5f,
                    Animation.RELATIVE_TO_SELF,0.5f)
            //取true时，指在AnimationSet中定义一个插值器（interpolater），
            // 它下面的所有动画共同。如果设为false，则表示它下面的动画自己定义各自的插值器。
            val setAnimation=AnimationSet(true)
            setAnimation.addAnimation(alphaAnimation)
            setAnimation.addAnimation(scaleAnimation)
            setAnimation.addAnimation(rotateAnimation)
            setAnimation.duration=5000
            setAnimation.fillAfter=true
            setAnimation.interpolator=BounceInterpolator()
            view.startAnimation(setAnimation)
        }
    }
}
